<template>
  <div class="roleList">
    <el-card>
      <div>
        角色管理
      </div>
      <el-button type="primary" style="margin: 20px 0" @click="addRole"><span style="font-size: 25px;">+</span>&nbsp&nbsp新增
      </el-button>
      <!--    表格-->
      <el-table style="width: 100%" :data="roleList" :row-style="{height:'50px'}">
        <el-table-column prop="role_id" label="角色ID" width="180"/>
        <el-table-column prop="role_name" label="角色名称" width="250"/>
        <el-table-column prop="sort" label="排序"/>
        <el-table-column prop="created_at" label="添加时间"/>
        <el-table-column label="操作">
          <template #default="scope">
            <span style="cursor: pointer;color: #1890ff;margin-right: 5px" @click="editRole(scope.row)">编辑</span>
            <span style="cursor: pointer;color: #1890ff" @click="deleteUser(scope.row)">删除</span>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px">
        <el-pagination
            v-model:currentPage="currentPage"
            background layout="prev, pager, next"
            :total="count"
            @current-change="changeSize"
            @prev-click="pageDown"
            @next-click="nextSize"
        />
      </div>
    </el-card>
    <RoleDialog ref="Role" :roleInfo="roleInfo" @EditSuccess="EditSuccess"></RoleDialog>
  </div>
</template>
<script setup>
import {ref} from "vue"
import RoleDialog from "@/view/Admin/RoleDialog";
import {getRoleLise,accountDelete} from "../../../HTTP/api";
import {onMounted} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {useRouter} from "vue-router";
import moment from "moment";
const router = useRouter()
//权限列表
let PermissionList = ref()
const Role = ref(null)
//遍历数据
const roleList = ref()
//总页数
const count = ref()
//当前页数
const currentPage = ref()
let roleInfo = ref()
function GetRoleList(page = 1, size = 10) {
  getRoleLise({page, size}).then(res => {
    if (res.code === 2000) {
      res.data.data.forEach(item => {
        item.created_at = moment(parseInt(item.created_at)).format('YYYY-MM-DD HH:MM')
      })
      roleList.value = res.data.data
      count.value = res.data.count
    }
    if (res.code === 3000) {
      sessionStorage.setItem("userToken", "")
      ElMessage.error('请重新登录')
      router.replace({path: '/login'})
    }
  })
}
//增加
const addRole = () => {
  roleInfo.value = ""
  Role.value.dialogFormVisible = true
}
//编辑
const editRole = (data) => {
  roleInfo.value = data
  Role.value.dialogFormVisible = true
}
//删除用户
const deleteUser = (data) => {
  // console.log(data)
  ElMessageBox.confirm(
      '你确定要删除该记录吗?',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        accountDelete({id:data.role_id}).then(res=>{
          console.log(res)
          if(res.code===2000){
            ElMessage({
              type: 'success',
              message: '操作成功',
            })
          }else {
            ElMessage.error("删除失败")
          }
        })
      })
      .catch(() => {
      })
}
//改变页数
const changeSize = (e) => {
  GetRoleList(e)
}
//上一页
const nextSize = () => {
  GetRoleList(currentPage + 1)
}
//下一页
const pageDown = () => {
  GetRoleList(currentPage - 1)
}
onMounted(() => {
  GetRoleList()
})
const EditSuccess = (e) => {
  if (e) {
    GetRoleList()
    currentPage.value=1
  }
}
</script>

<style scoped>
.roleList {
  flex: 1;
  margin: 20px;
}
</style>
